package cn.kgc.vue.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.kgc.vue.anno.LogAnnotation;
import cn.kgc.vue.common.Operation;
import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.dto.ScorePageDTO;
import cn.kgc.vue.entity.Score;
import cn.kgc.vue.service.ScoreService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author: 课工场
 * @since 2023/8/21
 */
@RestController
@RequestMapping("score")
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    @GetMapping("month")
    public ResponseResult getMonthList() {
        return scoreService.getMonthScore();
    }

    // 获取成绩分页数据
    @GetMapping("page")
    public ResponseResult getPage(ScorePageDTO scorePageDTO) {
        return scoreService.getPage(scorePageDTO);
    }

    //添加编辑成绩
    @PostMapping("saveOrUpdate")
    @LogAnnotation(model = "成绩模块",operation = Operation.UPDATE)
    @RequiresPermissions(value = {"base:score:add","base:score:update"},logical = Logical.OR)
    public ResponseResult addOrUpdate(@Validated @RequestBody Score score) {
        ResponseResult responseResult = new ResponseResult();
        //判定学生的成绩是否重复  同一时间不能出现相同学号的成绩
        LambdaQueryWrapper<Score> lambda = new QueryWrapper<Score>().lambda();

        lambda.eq(Score::getStudentNo,score.getStudentNo())
                .eq(ObjectUtils.isNotNull(score.getId()),Score::getScore,score.getScore())
                .eq(Score::getExamTime,score.getExamTime());

        List<Score> list = scoreService.list(lambda);
        if (list.size()>0) {
            responseResult.setMsg("考生成绩不能重复录入");
            responseResult.setCode(500);
            return responseResult;
        }
        scoreService.saveOrUpdate(score);
        responseResult.success();
        return responseResult;
    }

    // 根据id获取成绩信息
    @GetMapping("getById")
    public ResponseResult getById(Integer id) {
        ResponseResult responseResult = new ResponseResult();
        Score score = scoreService.getById(id);
        responseResult.success(ResponseStatus.SUCCESS,score);
        return responseResult;
    }
    // 根据id 删除学生成绩
    @DeleteMapping("delete")
    @LogAnnotation(model = "成绩模块",operation = Operation.DELETE)
    @RequiresPermissions(value = {"base:score:delete"})
    public ResponseResult delete(Integer id) {
        scoreService.removeById(id);
        ResponseResult responseResult = new ResponseResult();
        responseResult.success();
        return responseResult;
    }
    //批量添加学生成绩
    @PostMapping("batchAdd")
    @LogAnnotation(model = "成绩模块",operation = Operation.ADD)
    @RequiresPermissions(value = {"base:score:add"})
    public ResponseResult batchAdd(@RequestParam("file") MultipartFile file) throws Exception {
        return scoreService.batchAdd(file);
    }

    //导出学生成绩
    @GetMapping("excel")
    @LogAnnotation(model = "成绩模块",operation = Operation.SELECT)
    public void exportExcel(HttpServletResponse response,ScorePageDTO scorePageDTO) throws IOException {

        LambdaQueryWrapper<Score> lambda = new QueryWrapper<Score>().lambda();
        lambda.eq(StringUtils.isNotBlank(scorePageDTO.getClazz()),Score::getClazz,scorePageDTO.getClazz())
                .eq(ObjectUtils.isNotNull(scorePageDTO.getExamTime()),Score::getExamTime,scorePageDTO.getExamTime());

        List<Score> scores = scoreService.list(lambda);
        ExportParams exportParams = new ExportParams("学生成绩表", "成绩表");

        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Score.class, scores);
        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        workbook.close();
        outputStream.close();
    }
}
